package com.quanshu.exchange.support.db;

import com.j256.ormlite.dao.Dao;
import com.quanshu.exchange.support.db.core.DBController;
import com.quanshu.exchange.support.entity.GroupEntity;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by jye on 2017/12/15.
 */
public class GroupDao implements IDao<GroupEntity> {

    private volatile static GroupDao instance;

    public static GroupDao getInstance() {
        if (instance == null) {
            synchronized (GroupDao.class) {
                if (instance == null) {
                    instance = new GroupDao();
                }
            }
        }
        return instance;
    }

    private Dao<GroupEntity, String> getDao() throws SQLException {
        return DBController.getInstance().getDB().getDao(GroupEntity.class);
    }

    @Override
    public void save(GroupEntity entity) {
        try {
            getDao().createOrUpdate(entity);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void save(List<GroupEntity> list) {
        if (list == null || list.size() < 1) {
            return;
        }
        for (GroupEntity entity : list) {
            save(entity);
        }
    }

    @Override
    public void delete(GroupEntity entity) {
        try {
            getDao().delete(entity);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String primaryKey) {
        try {
            getDao().deleteById(primaryKey);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(GroupEntity entity) {
        try {
            getDao().update(entity);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public GroupEntity query(String primaryKey) {
        try {
            return getDao().queryForId(primaryKey);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GroupEntity> queryForAll() {
        try {
            return getDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
